<?php
namespace Admin\Controller;

class ChatAdController extends CommonController {
    public function index(){
        $this->assign('title','广告管理');
        $list = M('ad');
        $data = $list->select();
        $this->assign('data',$data);
        $this->show();
    }
    public function add(){
         // var_dump($_POST);
          // 得到自定的模型类
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728 ;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->replace = 'true';//存在同名图片 覆盖
                $upload->subName = 'ad';
                // 上传文件
                $info = $upload->upload();
                // var_dump($info);
                if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    // foreach($info as $file){
                    $head = $info['adpic']['savename'];
                    // var_dump($head);
                }
                $ad = M('ad');
                // 取得成功上传的文件信息
                $info = $upload->upload();

                // 保存当前数据对象
                // $ad->
                $ad->ad_name = $_POST['adname'];
                $ad->ad_address = $head;
                $ad->ad_link = $_POST['adlink'];
                $re = $ad->add();
                // echo $ad->getLastsql();die;
                 if($re>0){
               $this->success();
                 }else{
                    $this->error('fail');
                    return false;
                 }
        }
        public function del(){
            $ad = M('ad');
             $re = $ad->delete($_POST['ad_id']);
             if($re){
                echo '1';
             }else{
                echo '2';
             }
        }
        public function e(){
        $Model = new \Think\Model();
         $res = $Model->execute("update dota2_ad set ad_status = 0");
        $info = M('ad');
        $id = $_POST['id'];
        $data['ad_status'] = $_POST['up'];
        $where['ad_id'] = $_POST['id'];
        $re = $info->where($where)->save($data);

        }
        public function edit(){
             $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728 ;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->replace = 'true';//存在同名图片 覆盖
                $upload->subName = 'ad';
                // 上传文件
                $info = $upload->upload();
                    $ad = M('ad');
                    if($_POST['ad_name'] != null){
                        $data['ad_name'] = $_POST['ad_name'];
                    }
                    if($_POST['ad_link'] != null){
                        $data['ad_link'] = $_POST['ad_link'];
                    }
                    $where['ad_id'] = $_POST['ad_id'];
                    if($info!= null){
                    $head = $info['adpic']['savename'];
                        $data['ad_address'] = $head;
                    }
                    $re = $ad->where($where)->save($data);
                    if($re>0){
                   $this->redirect('Admin/chatAd/index');
                    }else{
                    return false;
                    }
    }
}
   